#include "stdafx.h"
#include "Value_BeStealthy.h"
#include "emotionalmemory.h"
#include "event.h"
#include "role.h"
#include "emotions.h"

using namespace BAA;



void BAA::Value_BeStealthy::Create()
{

}

EmotionalMemory * BAA::Value_BeStealthy::AppraiseEvent(Event * event)
{

	// get how loud the event was.

	StringFloatMap * map = event->getConsequences();

	float loudness = (*(event)->getConsequences())["loudness"];

	float loudnessSensitivity =  mStringFloatData["loudness"];
	float combatSensitivity = mStringFloatData["CombatSensitivity"];

	EmotionalMemory * mem = NULL;
	// is the event loud enough to cause an event?
	if(loudness > loudnessSensitivity)
	{
		mem = new EmotionalMemory();
		mem->Age(0);
		mem->RoleParent(this->RoleParent());
		mem->SetEvent(event);
		


		// check to see if a threat has been noted within the role
		// be more sensitive if in combat
		float combatVal = (combatSensitivity ? (*(RoleParent()->Self()->Properties()->StateMap()))["InCombat"]: 1.0f);

		// if the event was caused from self
		if(event->getTrigger() == RoleParent()->Self())
		{		
			Emotion * fear = new Emotion_Fear();

			if(fear != NULL)
			{
				fear->InitializeValue((loudnessSensitivity-loudness)*combatVal);
				mem->AddEmotion(fear->Name(), fear);
			}

		}
		else // event was caused my another in the group? If so be scared and angry
		{
			for(unsigned int i = 0; i < event->getTargets()->size(); ++i)
			{			
				AgentPointerMap * targets = RoleParent()->Targets();
				//if(mRole->Targets()[event->trigger] != NULL)
				if((*targets)[event->getTrigger()] != NULL)
				{
					Emotion * emotion = new Emotion_Fear();

					if(emotion != NULL)
					{

						emotion->InitializeValue((loudnessSensitivity-loudness)*combatVal);
						mem->AddEmotion(emotion->Name(), emotion);

						emotion = new Emotion_Anger();
						emotion->InitializeValue((loudnessSensitivity-loudness)*combatVal);
						mem->AddEmotion(emotion->Name(), emotion);

					}
					break;
				}
			}// end for targets
		}// end if self



	}// end if loudness


	return mem;

}

